/**
 * Generic tab change handler that retrieves tab content based on tab label
 * 	- replaces current tab content holder with "Loading..." then delegate to Spring remoting to retrieve tab content
 */
var handleTabChange = function() {
	var tabName = tabView.get('activeTab').get('href').toLowerCase();
	getTabContent(tabName);
	
};

/**
 * Utility methed used to retrieve tab content via ajax
 * @param url - Url for content to retreive
 * @param params object literal containing name/value pairs of params to be passed  while retrieving content
 */
var getTabContent = function(url, params) {
	
	$('tabContent').update('Loading Data...');
	if (!params) {
		// Spring remoting requires at least empty object
		params = {};
	}
	params.r = new Date().getTime();
	Spring.remoting.getResource(url, params);
	
};

function getTimestamp(){
	return new Date().getTime();
}
// YUI TabView widget. Defined in global scope because it's accessed from multiple methods
// TODO: wrap tab related vars/functions in container object to limit scope
var tabView;
var initTabView = function() {

	tabView = new YAHOO.widget.TabView();
	tabView.addTab(new YAHOO.widget.Tab({label: 'Branch', href: 'LOCATION'}));
	tabView.addTab(new YAHOO.widget.Tab({label: 'Menu', href: 'MENU'}));
	tabView.addTab(new YAHOO.widget.Tab({label: 'Category', href: 'CATEGORY'}));
	tabView.addTab(new YAHOO.widget.Tab({label: 'Reviews', href: 'REVIEWS'}));
	tabView.addTab(new YAHOO.widget.Tab({label: 'Events', href: 'EVENTS'}));
	tabView.addTab(new YAHOO.widget.Tab({label: 'Offers', href: 'OFFERS'}));
	tabView.addTab(new YAHOO.widget.Tab({label: 'Photo Gallery', href: 'GALLERY'}));
	tabView.appendTo('admin-tabs'); 

	// Only load default tab page when tab is actually "clicked"
	YEvent.delegate("admin-tabs", "click", handleTabChange, 'a');

	// initialSelectedTab comes from server side model attribute
	goToTab($F('initialSelectedTab'));
	handleTabChange();
	
};

var goToTab = function(tabName) {
	
	// Note: This function *only* changes the active  tab, it no longer loads the selected tabs content
	tabView.get("tabs").each(function(tab, tabIndex){
		if (tab.get("href") == tabName) {
			tabView.selectTab(tabIndex);
		}
	});
	
};

function selectTabAndGetTabContent(tabName) {
	// Note: This function *only* changes the active  tab, it no longer loads the selected tabs content
	tabView.get("tabs").each(function(tab, tabIndex){
		if (tab.get("href") == tabName) {
			tabView.selectTab(tabIndex);
		}
	});
	
	handleTabChange();
}

/**
 * Utility method used to toggle active/inactive class for a given row
 * @param row
 * @returns
 */
var markRowActive = function(row) {
	YDom.removeClass(row, "inactive");
	YDom.addClass(row, "active");
	// get all div elements that are in this row
    rowDivs = row.getElementsByTagName("div");

    // get the first div = inactive icon div

    if (rowDivs.length > 0){
    	inactiveIconDiv = rowDivs[0];
		rowDivs[0].className = "";
    }
};

/**
 * Utility method used to toggle active/inactive class for a given row
 * @param row
 * @returns
 */
var markRowDeactive = function(row) {
	YDom.removeClass(row, "active");
	YDom.addClass(row, "inactive");
	// get all div elements that are in this row
    rowDivs = row.getElementsByTagName("div");

    // get the first div = inactive icon div

    if (rowDivs.length > 0){
    	inactiveIconDiv = rowDivs[0];
		rowDivs[0].className = "inactive-user-icon";
    }
};

/**
 * Utility method used to retrieve the parent TR for a given element.
 * @param element
 * @returns *prototype* decorated instance of parent TR
 */
var getParentRow = function(element) {
	return $(element).up("tr");
};



